package cn.wyj.system.service.impl;

import cn.wyj.basic.query.BaseQuery;
import cn.wyj.basic.util.PageList;
import cn.wyj.system.domain.Menu;
import cn.wyj.system.domain.Role;
import cn.wyj.system.mapper.MenuMapper;
import cn.wyj.system.mapper.PermissionMapper;
import cn.wyj.system.mapper.RoleMapper;
import cn.wyj.system.service.IRoleService;
import cn.wyj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.event.ItemListener;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public void add(Role role) {
        super.add(role);
        // 添加角色权限中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
        // 添加角色菜单中间表
        menuMapper.addMiddle(role.getId(), role.getMenus());

    }

    @Override
    public void update(Role role) {
        super.update(role);
        // 在添加角色权限之前，要先将该角色的权限都删除，操作的是role_permission表，写再roleMapper
        permissionMapper.deleteMiddle(role.getId());
        // 删除之后，再给角色权限中间表，添加角色权限
        permissionMapper.addMiddle(role.getId(), role.getPermissions());
        // 在添加角色菜单权限之前，先删除菜单权限，操作的是role_menu表
        menuMapper.deleteByMiddle(role.getId());
        // 删除之后再添加
        menuMapper.addMiddle(role.getId(), role.getMenus());



    }
//        @Override
//    public PageList<Role> queryByPage(BaseQuery query) {
//        PageList<Role> rolePageList = super.queryByPage(query);
//        List<Role> roleList = rolePageList.getRows();
//        // 将roleid收集起来
//        List<Long> roleIds = roleList.stream().map(Role::getId).collect(Collectors.toList());
////        // 根据角色ID查权限
////        List<Long> perms =  permissionMapper.findByIds();
//        for (Long roleId : roleIds) {
//            // 根据角色id查菜单
//            List<Menu> menus = menuMapper.findAllByIds(roleId);
//            for (Role role : roleList) {
//                if (role.getId())
//            }
//        }
//
//        return null;
//    }
}
